package cetcbigdata.da.business_environment.gansu;

import cetcbigdata.da.base.Base;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Element;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class SpiderGS extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject parseListPage() {

        //地区遍历
        StringBuilder region = new StringBuilder();
        region.append("甘肃省-");
        //市
        String SJUrl = "https://zwfw.gansu.gov.cn/szgs/navigationInterface/searchNavigationByCode.do?areaCode=620000000000";
        JSONObject SJHeader = new JSONObject();
        SJHeader.put("Referer", "https://zwfw.gansu.gov.cn/szgs/sxcx/personal/theme/showIndex.do");
        SJHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
//        JSONObject SJParmas = new JSONObject();
        String SJStrResponse = getReq(SJUrl,SJHeader,new JSONObject());
        JSONObject SJResponse = JSONObject.parseObject(SJStrResponse);
        for (Object SJReqParmas_ : SJResponse.getJSONObject("params").getJSONArray("navigationList")){
            JSONObject SJReqParmas = (JSONObject) SJReqParmas_;

            region.replace(4,region.length(),SJReqParmas.getString("name")).append("-");
            int SSLength = region.length();
            //区
            String QJUrl = "https://zwfw.gansu.gov.cn/szgs/navigationInterface/searchNavigationByCode.do?areaCode=" + SJReqParmas.getString("code");//620100000000
            JSONObject QJHeader = new JSONObject();
            QJHeader.put("Referer", "https://zwfw.gansu.gov.cn/szgs/sxcx/personal/theme/showIndex.do");
            QJHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
            String QJStrResponse = getReq(QJUrl,QJHeader,new JSONObject());
            JSONObject QJResponse = JSONObject.parseObject(QJStrResponse);
            for (Object o1 : QJResponse.getJSONObject("params").getJSONArray("navigationList")){
                JSONObject QJReqParmas = (JSONObject) o1;

                region.replace(SSLength,region.length(),QJReqParmas.getString("name"));

                String listUrl = QJReqParmas.getString("map") + "frfw/asmzq/index.html";
                JSONObject listHeader = new JSONObject();
                listHeader.put("Referer", QJReqParmas.getString("map") + "frfw/asmzq/index.html");
                listHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
                String listResponse = getReq(listUrl,listHeader,new JSONObject());
                listResponse = new String(listResponse.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
                JXDocument jxDocument = new JXDocument(listResponse);
                List<Object> clearfixs = jxDocument.sel("//div[@class='smzq_con']/div");
                for (Object clearfix : clearfixs){
                    Element title = ((Element) clearfix).child(0).child(0);
                    String titleUrl = "https://zwfw.gansu.gov.cn" + title.attr("href");
                    String titleResp = getReq(titleUrl, null, null);
                    titleResp = new String(titleResp.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
                    JXDocument jxDocument1 = new JXDocument(titleResp);
                    JSONObject result = new JSONObject();
                    JSONArray pageList = new JSONArray();
                    List<Object> bodys = jxDocument1.sel("//div[@class='main_con clearfix']/a");
                    for (Object o : bodys){
                        Element body = ((Element) o);
                        JSONObject pageResult = new JSONObject();
                        pageResult.put("名称", body.text());
                        pageResult.put("url", "https://zwfw.gansu.gov.cn" + body.attr("href"));
                        pageList.add(pageResult);
                    }
                    result.put("生命周期名称", title.text());
                    result.put("Url", titleUrl);
                    result.put("选项", pageList);
//                    System.out.println(title.text()+region+"\n"+result);
                    return processResult(
                            listUrl,
                            listResponse,
                            "",
                            "",
                            result,
                            region.toString(),
                            "甘肃省营商环境-"+title.text()
                    );
                }
            }
        }
        return null;
    }

    public static void main(String[] args) {
        SpiderGS spiderGS = new SpiderGS();
        spiderGS.getDataDemo();
    }
}